<?php	 	 

/**
 * This is the model class for table "lhp".
 *
 * The followings are the available columns in table 'lhp':
 * @property integer $id
 * @property string $satker
 * @property string $nama_data
 * @property string $deskripsi_data
 * @property string $status
 * @property string $lampiran
 * @property integer $pembuat_data
 * @property string $tanggal_pembuatan
 *
 * The followings are the available model relations:
 * @property Users $pembuatData
 */
class Lhp extends CActiveRecord
{
	public $provinsi;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Lhp the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'lhp';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nama_data, lampiran', 'required'),
			array('pembuat_data', 'numerical', 'integerOnly'=>true),
			array('satker', 'length', 'max'=>25),
			array('status', 'SWValidator'),
			array('nama_data, lampiran', 'length', 'max'=>255),
			array('deskripsi_data, status, tanggal_pembuatan', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, provinsi, satker, nama_data, deskripsi_data, status, lampiran, pembuat_data, tanggal_pembuatan', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'pembuatData' => array(self::BELONGS_TO, 'Users', 'pembuat_data'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'satker' => 'Satker',
			'nama_data' => 'Nama Data',
			'deskripsi_data' => 'Deskripsi Data',
			'status' => 'Status',
			'lampiran' => 'Lampiran',
			'pembuat_data' => 'Pembuat Data',
			'tanggal_pembuatan' => 'Tanggal Pembuatan',
			'provinsi' => 'Provinsi',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$id_satker = Yii::app()->user->getState('satker');
		$criteria=new CDbCriteria;
		
		
		if(Yii::app()->user->checkAccess('satker')){
			$criteria->condition = ' satker='.$id_satker;
		}
		if(Yii::app()->user->checkAccess('wilayah')){
			$criteria->condition = ' (status="swLhp/Siap"  OR status="swLhp/Reviewulang") AND satker LIKE "'.$id_satker.'%"';
		}
		if(Yii::app()->user->checkAccess('eselon1')){
		if(!empty($this->provinsi)){
			$criteria->join = 'JOIN satker ON t.satker = satker.satker_kode JOIN provinsi ON satker.provinsi_id = provinsi.id';
			$criteria->condition = 'provinsi.id='.$this->provinsi.' AND (status="swLhp/Kirimkeelselon1") ';
		}
		else{
		$criteria->join = 'JOIN satker ON t.satker = satker.satker_kode JOIN provinsi ON satker.provinsi_id = provinsi.id';
			$criteria->condition = 'status="swLhp/Kirimkeelselon1"';
		}	
		}
		if(Yii::app()->user->checkAccess('lembaga')){
		if(!empty($this->provinsi)){
			$criteria->join = 'JOIN satker ON t.satker = satker.satker_kode JOIN provinsi ON satker.provinsi_id = provinsi.id';
			$criteria->condition = 'provinsi.id='.$this->provinsi.' AND (status="swLhp/Approve") ';
		}
		else{
		$criteria->join = 'JOIN satker ON t.satker = satker.satker_kode JOIN provinsi ON satker.provinsi_id = provinsi.id';
			$criteria->condition = 'status="swLhp/Approve"';
		}
		}
		if(Yii::app()->user->checkAccess('pemeriksa')){
		if(!empty($this->provinsi)){
			$criteria->join = 'JOIN satker ON t.satker = satker.satker_kode JOIN provinsi ON satker.provinsi_id = provinsi.id';
			$criteria->condition = 'provinsi.id='.$this->provinsi.' AND (status="swLhp/Publish") ';
		}
		else{
		$criteria->join = 'JOIN satker ON t.satker = satker.satker_kode JOIN provinsi ON satker.provinsi_id = provinsi.id';
			$criteria->condition = 'status="swLhp/Publish"';
		}
		}
		
		$criteria->compare('id',$this->id);
		$criteria->compare('satker',$this->satker,true);
		$criteria->compare('nama_data',$this->nama_data,true);
		$criteria->compare('deskripsi_data',$this->deskripsi_data,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('lampiran',$this->lampiran,true);
		$criteria->compare('pembuat_data',$this->pembuat_data);
		$criteria->compare('tanggal_pembuatan',$this->tanggal_pembuatan,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function behaviors()
	{
	    return array(
	        'swBehavior'=>array(
	            'class' => 'application.extensions.simpleWorkflow.SWActiveRecordBehavior',
	        ),
	    );
	}
}